﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using FineUI;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Org;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.OrganizationalLife
{
    public partial class MemberCostEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyMemberCostAdd";
            }
        }
        /// <summary>
        /// 年
        /// </summary>
        public string Year
        {
            get { return GetQueryIntValue("Year"); }

        }
        /// <summary>
        /// 月
        /// </summary>
        public string Month
        {
            get { return GetQueryIntValue("Month"); }
        }
        /// <summary>
        /// 用户ID
        /// </summary>
        public string LoginID
        {
            get { return GetQueryIntValue("LoginID"); }
        }

        /// <summary>
        /// id
        /// </summary>
        public string Id
        {
            get { return GetQueryIntValue("id"); }
        }
        #endregion

        #region 页面属性
        public string EditorContent
        {
            get; set;
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            var year = DateTime.Now.Year;
            for (int i = year - 5; i <= year + 5; i++)
            {
                this.ddlYear.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
            }
            for (int i =1; i <= 12; i++)
            {
                this.ddlMonth.Items.Add(new ListItem() { Text = i+"月", Value = i.ToString() });
            }
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {
            this.dtPayDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            if (string.IsNullOrEmpty(Id))
            {
                if (string.IsNullOrEmpty(Year) || string.IsNullOrEmpty(Month) || string.IsNullOrEmpty(LoginID))
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                this.ddlMonth.SelectedValue = Month;
                this.ddlYear.SelectedValue = Year;
                this.txtName.Text = DB.PartyOrgMembers.Find(LoginID).UserName;
                txtCost.Text = PartyOrgMemberSetting.GetSettingValue(LoginID, "MemberCostBase"); ;
            }
            else
            {
                var current = DB.PartyMemberCosts.Find(Id);
                this.ddlMonth.SelectedValue = current.Month;
                this.ddlYear.SelectedValue = current.Year;
                this.txtName.Text = DB.PartyOrgMembers.Find(current.LoginID).UserName;
                this.txtCost.Text = current.Cost.ToString(CultureInfo.InvariantCulture);
                this.dtPayDate.Text = Convert.ToDateTime(current.PayDate).ToString("yyyy-MM-dd");
            }


        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            
            PartyMemberCost current;
            if (id != "")
            {
                //更新
                current = DB.PartyMemberCosts.Find(id);
                if (current == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                current.ModifyTime = DateTime.Now;
                current.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                current = new PartyMemberCost();
                current.ID = StringHelper.NewID;
                current.LoginID = LoginID;
                current.Status = 4;
                current.CreateTime = DateTime.Now;
                current.ModifyTime = DateTime.Now;
                current.CreateBy = CurrentUser.LoginID;
                current.ModifyBy = CurrentUser.LoginID;
                DB.PartyMemberCosts.Add(current);
            }

            current.Month = ddlMonth.SelectedValue;
            current.PayDate = Convert.ToDateTime(dtPayDate.Text);
            current.PayWay = ddlPayWay.SelectedValue;
            current.Year = ddlYear.SelectedValue;
            current.Cost = Convert.ToDecimal( txtCost.Text);
            if (DB.SaveChanges()>0)
            {
                //增加积分
                PartyScoreHelper.Add(() => PartyScoreHelper.AddMemberCost(current.LoginID, CurrentUser.LoginID, current.ID,string.Format("{0}-{1}-01", Year,Month)));
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

      
        #endregion
    }
}